﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using LMSemir.DAC;
using LMSemir.Common;

namespace LMSemir.API
{
    public partial class uploadimg2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string teeID = WebUtils.GetPramaValue("tid", "0");

            if (!string.IsNullOrEmpty(teeID) && Convert.ToInt32(teeID) > 0)
            {
                #region 检查图片存放路径
                string phsisPath = Server.MapPath("~/images/AI/");
                string yearPath = phsisPath + DateTime.Now.Year + "\\";
                if (!Directory.Exists(yearPath))
                {
                    Directory.CreateDirectory(yearPath);
                }
                string monthPath = yearPath + DateTime.Now.Month + "\\";
                if (!Directory.Exists(monthPath))
                {
                    Directory.CreateDirectory(monthPath);
                }
                string dayPath = monthPath + DateTime.Now.Day + "\\";
                if (!Directory.Exists(dayPath))
                {
                    Directory.CreateDirectory(dayPath);
                }

                string largeDir = dayPath;
                if (!Directory.Exists(largeDir))
                {
                    Directory.CreateDirectory(largeDir);
                }
                #endregion

                #region 保存图片并存储到数据库
                //获取最终小,中,大图片存储路径
                string tempFileName = largeDir + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "-" + teeID + ".jpg";

                Stream stream = Request.InputStream;

                if (ImageUtils.saveImgByPath(tempFileName, stream))
                {
                    string tempUrl = "http://" + Request.Url.Host + "/2011tees" + ImageUtils.getImageUrl(tempFileName);

                    string sqlUpdate = string.Format("update tbteeinfo set tlargeimage='{0}' where tid={1}", tempUrl, teeID);
                    try
                    {
                        int iUpdate = SqlUtil.ExecteNonQuery(System.Data.CommandType.Text, sqlUpdate);
                        if (iUpdate == 1)
                        {
                            this.Response.Write(TEEUtil.generateTeeXML(1, "imgurl", tempUrl));
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Response.Write(TEEUtil.generateTeeXML(-1, "error", ex.Message));
                        throw;
                    }
                }
                #endregion 
            }
            else
            {
                this.Response.Write(TEEUtil.generateTeeXML(-1, "error", "teeID is not null"));
            }
        }
    }
}